package com.wx.service.impl;

import com.wx.dto.ForgotPwdDTO;
import com.wx.dto.RefineUserInfo;
import com.wx.dto.UpdatePwd;
import com.wx.dto.UserUpdateDTO;
import com.wx.entity.User;
import com.wx.exception.BaseException;
import com.wx.mapper.UserMapper;
import com.wx.service.UserService;
import com.wx.vo.UserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /*用户注册账号*/
    @Override
    public void register(User user) {
        //查询用户名是否重复
        User user1 = userMapper.queryByUsername(user.getUsername());
        if(user1 != null){
            /*用户名重复，抛异常*/
            throw new BaseException("用户名重复");
        }
        //md5加密
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Password);
        //补全信息{name}
        user.setCreateTime(LocalDateTime.now());
        user.setIsDelete(0);
      /*
        user.setName("购车用户");
        user.setEmail("buy@car.com");
        user.setPhone("00000000000");
        user.setAddress("中国");
       */

        userMapper.saveUser(user);
    }

    /*用户登录*/
    @Override
    public UserLoginVO login(User user) {
        User user1 = userMapper.queryByUsername(user.getUsername());
        if(user1 == null){
            /*用户名不存在，抛异常*/
            throw new BaseException("用户名不存在");
        }

        //判断用户账号是否注销
        if(user1.getIsDelete() == 1){
            //账号已注销，用户名不可用
            throw new BaseException("用户名不可用");
        }

        //验证密码是否正确
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!md5Password.equals(user1.getPassword())){
            //密码错误
            throw new BaseException("密码错误");
        }

        //赋值相同字段名
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(user1,userLoginVO);

        return userLoginVO;
    }

    /*用户修改个人信息*/
    @Override
    public void update(UserUpdateDTO userUpdateDTO) {
        //字符串切片，获取type和data
        String[] strs = userUpdateDTO.getKey().split(";");
        userUpdateDTO.setType(strs[0]);
        userUpdateDTO.setData(strs[1]);
        System.out.println(userUpdateDTO.getType()+":"+userUpdateDTO.getData());
        //构建user类
        User user = User.builder()
                .username(userUpdateDTO.getUsername())
                .build();
        //判断type类型
        if(userUpdateDTO.getType().equals("name")){
            user.setName(userUpdateDTO.getData());
        }else if(userUpdateDTO.getType().equals("email")){
            user.setEmail(userUpdateDTO.getData());
        }else if(userUpdateDTO.getType().equals("phone")){
            user.setPhone(userUpdateDTO.getData());
        }else{
            user.setAddress(userUpdateDTO.getData());
        }

        userMapper.updateUser(user);
    }

    /*用户修改密码*/
    @Override
    public void updatePwd(UpdatePwd pwd) {
        //检查用户密码是否正确
        User user1 = userMapper.queryByUsername(pwd.getUsername());
        String md5Password = DigestUtils.md5DigestAsHex(pwd.getOldPassword().getBytes());
        if(!md5Password.equals(user1.getPassword())){
            throw new BaseException("原密码不正确");
        }
        //构造user类,修改密码（md5加密）
        String newPassword = DigestUtils.md5DigestAsHex(pwd.getNewPassword().getBytes());
        User user = User.builder()
                .username(pwd.getUsername())
                .password(newPassword)
                .build();
        userMapper.updateUser(user);

    }

    /*用户完善个人信息*/
    @Override
    public void refine(RefineUserInfo refineUserInfo) {
        userMapper.refineInfo(refineUserInfo);
    }

    /*用户注销账号*/
    @Override
    public void deleteUser(User user) {
        //判断用户密码是否正确
        User user1 = userMapper.queryByUsername(user.getUsername());
        String mdPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!user1.getPassword().equals(mdPassword)){
            throw new BaseException("密码错误，取消注销");
        }
        //注销
        userMapper.deleteUser(user);
    }

    /*用户忘记重置密码*/
    @Override
    public void forgotpwd(ForgotPwdDTO forgotPwdDTO) {
        //根据用户名查询账号
        User user1 = userMapper.queryByUsername(forgotPwdDTO.getUsername());
        if(user1 == null){
            throw new BaseException("用户名不存在");
        }
        //比较邮箱
        if(!user1.getEmail().equals(forgotPwdDTO.getEmail())){
            throw new BaseException("邮箱不正确");
        }
        //比较手机号
        if(!user1.getPhone().equals(forgotPwdDTO.getPhone())){
            throw new BaseException("手机号不正确");
        }
        //md5加密密码
        String mdPassword = DigestUtils.md5DigestAsHex(forgotPwdDTO.getNewPassword().getBytes());
        User user = User.builder()
                .username(forgotPwdDTO.getUsername())
                .password(mdPassword)
                .build();
        userMapper.updateUser(user);
    }

    /*清除用户购物车和收藏数据*/
    @Override
    public void purge(User user) {
        userMapper.purge(user);
    }

}
